using System;
using System.Collections.Generic;
using System.Text;
using MSOutlook = Microsoft.Office.Interop.Outlook;
using OfficeCore = Microsoft.Office.Core;   
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;

namespace OutlookSharePointExplorer
{
    /// <summary>
    /// This class holds functions and properties that are equal to all Items.
    /// </summary>
    internal class OutlookInspector
    {

        #region Events 
        
		/// <summary>
		/// When Outlook Inspector is closed, we send an Event to ActiveInspectors
		/// </summary>
        public event OutlookInspectorDelegate Inspector_Closed;

        /// <summary>
        /// When Outlook Inspector is activated, we send an Event to ActiveInspectors
        /// </summary>
        public event OutlookInspectorDelegate Inspector_Activated;

        /// <summary>
        /// When Outlook Inspector is deactivated, we send an Event to ActiveInspectors
        /// </summary>
        public event OutlookInspectorDelegate Inspector_Deactivated;
			
		#endregion

		#region Private Variables

		/// <summary>
		/// Object for COM interop
		/// </summary>
		protected object _Missing = System.Reflection.Missing.Value ;  

        /// <summary>
        /// The unique ID of our Inspector
        /// </summary>
        protected int _ID = 0;

        /// <summary>
        /// Indicates if this Item is a new Item
        /// </summary>
        protected bool _IsNew = false;

        /// <summary>
        /// A unique Identifier for our Business Object
        /// </summary>
        protected string _OUTLOOKUID = "";

        /// <summary>
        /// This flag is used to prevent the Save Dialog when nothing has changed.
        /// </summary>
        protected bool _IsInitializing = true;

        /// <summary>
        /// The Outlook Inspector that we are wrapping
        /// </summary>
        protected MSOutlook.InspectorClass _Inspector = null;


        #endregion 

        #region Public Properties

        /// <summary>
        /// The unique ID of our Inspector
        /// </summary>
        public int ID
        {
            get { return _ID; }
        }

        /// <summary>
        /// A unique Identifier for our Business Object
        /// </summary>
        public string OUTLOOKUID
        {
            get { return _OUTLOOKUID; }
        }

        /// <summary>
        /// Indicates if this Item is a new Item
        /// </summary>
        public bool IsNew
        {
            get { return _IsNew; }
            set { _IsNew = value; }
        }

        #endregion

        #region Eventhandlers

        /// <summary>
        /// This function should be called when the Item Open event is raised.
        /// Then the Inspector object is accessible and we can register for the close event.
        /// </summary>
        protected void ItemOpened()
        {
            try
            {
                // register for the Inspector events here.
                _Inspector.InspectorEvents_10_Event_Close += new MSOutlook.InspectorEvents_10_CloseEventHandler(_Inspector_InspectorEvents_10_Event_Close);
                _Inspector.InspectorEvents_Event_Activate += new MSOutlook.InspectorEvents_ActivateEventHandler(_Inspector_InspectorEvents_Event_Activate);
                _Inspector.InspectorEvents_Event_Deactivate += new MSOutlook.InspectorEvents_DeactivateEventHandler(_Inspector_InspectorEvents_Event_Deactivate);
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        void _Inspector_InspectorEvents_Event_Deactivate()
        {
            OnDeactivated();
            InspectorDeactivated();
        }

        void _Inspector_InspectorEvents_Event_Activate()
        {
            OnActivated();
            InspectorActivated();
        }

        /// <summary>
        /// this is the eventhandler for the Inspector CLose event.
        /// </summary>
        void _Inspector_InspectorEvents_10_Event_Close()
        {
            OnClosed();
            InspectorClosed();
            
        }

        /// <summary>
        /// This function is called when our Inspector has closed
        /// </summary>
        void InspectorClosed()
        {
            // send an event to ActiveInspectors Collection,
            // so we are released from Memory
            if (Inspector_Closed != null)
            {
                Inspector_Closed(_ID);
            }
            
            // unregister the events
            _Inspector.InspectorEvents_10_Event_Close -= new MSOutlook.InspectorEvents_10_CloseEventHandler(_Inspector_InspectorEvents_10_Event_Close);
            _Inspector.InspectorEvents_Event_Activate -= new MSOutlook.InspectorEvents_ActivateEventHandler(_Inspector_InspectorEvents_Event_Activate);
            _Inspector.InspectorEvents_Event_Deactivate -= new MSOutlook.InspectorEvents_DeactivateEventHandler(_Inspector_InspectorEvents_Event_Deactivate);

            // and release the Inspector object, else Outlook hangs around
            Marshal.ReleaseComObject(_Inspector);
        }

        /// <summary>
        /// send an event to ActiveInspectors Collection,
        /// we can track the last activated Inspector
        /// </summary>
        void InspectorActivated()
        {
            // send an event to ActiveInspectors Collection,
            if (Inspector_Activated != null)
            {
                Inspector_Activated(_ID);
            }
        }

        /// <summary>
        /// send an event to ActiveInspectors Collection,
        /// we can track the last deactivated Inspector
        /// </summary>
        void InspectorDeactivated()
        {
            if (Inspector_Deactivated != null)
            {
                Inspector_Deactivated(_ID);
            }
        }

        #endregion

        #region Virtual Functions
        // that can be overriden by derived classes to trap those events

        protected virtual void OnClosed() { }

        protected virtual void OnActivated() { }

        protected virtual void OnDeactivated() { }

        #endregion


    }
}
